home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / fastmergelogs.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-02  |  3.1 KB  |  142 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20. #include "memstat.h"
  21.  
  22. #include <assert.h>
  23. #include <iostream.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27.  
  28.  
  29. void mergelogs (int nlogs,
  30.         FILE ** f,
  31.         FILE * output)
  32. {
  33.   int * eof = new int[nlogs];
  34.   int * length = new int[nlogs];
  35.   MemoryRequest * m = new MemoryRequest[nlogs];
  36.  
  37.   int neofs = 0;
  38.   int totallength = 0;
  39.  
  40.   // Read in the first line of each.
  41.   for (int i = 0; i < nlogs; i++) {
  42.     fread (&length[i], sizeof(int), 1, f[i]);
  43.     if (length[i] == 0) {
  44.       eof[i] = 1;
  45.       neofs++;
  46.     } else {
  47.       eof[i] = 0;
  48.       fread (&m[i], sizeof(MemoryRequest), 1, f[i]);
  49.     }
  50.     totallength += length[i];
  51.   }
  52.  
  53.   fwrite (&totallength, sizeof(int), 1, output);
  54.  
  55.   while (neofs < nlogs) {
  56.  
  57.     printf (".");
  58.  
  59.     // Find out which files are finished.
  60.     for (int i = 0; i < nlogs; i++) {
  61.       if (!eof[i] && feof(f[i])) {
  62.     eof[i] = 1;
  63.     neofs++;
  64.       }
  65.     }
  66.  
  67.     if (neofs < nlogs) {
  68.  
  69.       // Find the least element.
  70.       MemoryRequest least;
  71.       int leastElement = -1;
  72.       for (int i = 0; i < nlogs; i++) {
  73.     if (!eof[i]) {
  74.       if (m[i] < least) {
  75.       if (m[i] == least) {
  76.         if ((m[i].getType() == MemoryRequest::MALLOC_OP)
  77.         || (m[i].getType() == MemoryRequest::REALLOC_OP)
  78.         || (m[i].getType() == MemoryRequest::ALLOCATE_OP)) {
  79.           least = m[i];
  80.           leastElement = i;
  81.         }
  82.       } else {
  83.         least = m[i];
  84.         leastElement = i;
  85.       }
  86.       }
  87.     }
  88.       }
  89.       assert (leastElement > -1);
  90.       
  91.       fwrite (&least, sizeof(MemoryRequest), 1, output);
  92.  
  93.       // Read one more element.
  94.       fread (&m[leastElement], sizeof(MemoryRequest), 1, f[leastElement]);
  95.     }
  96.   }
  97. }
  98.  
  99.  
  100. int main ()
  101. {
  102.   FILE ** f;
  103.  
  104.   int i;
  105.  
  106.   int from = 0;
  107.   int to = 127;
  108.  
  109.   // nlogs = the number of logs.
  110.   int nlogs = to - from + 1;
  111.  
  112.   f = new (FILE *)[nlogs];
  113.  
  114.   //
  115.   // Open all the files.
  116.   //
  117.  
  118.   // Open the logs.
  119.   for (i = from; i <= to; i++) {
  120.     char fname[255];
  121.     sprintf (fname, "log%d", i);
  122.     f[i] = fopen (fname, "r");
  123.   }
  124.  
  125.   // Open the output log.
  126.   FILE * output = fopen ("output", "w+");
  127.  
  128.   // Process them.
  129.   mergelogs (nlogs, f, output);
  130.  
  131.   // Close all the files.
  132.   for (i = from; i <= to; i++) {
  133.     fclose (f[i]);
  134.   }
  135.   fclose (output);
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.